package com.jietong.rfid.ui.params_set;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import com.jietong.rfid.ui.R;
import com.jietong.rfid.uhf.service.R2000ReaderService;
import com.jietong.rfid.uhf.service.impl.R2000ReaderServiceImpl;
import com.jietong.rfid.ui.util.ReaderUtil;
import com.jietong.rfid.ui.util.Toasts;
import com.jietong.rfid.util.Regex;

public class WorkPatternActivity extends Activity implements OnClickListener,OnItemSelectedListener {
	private Spinner spinnerWorkPattern;
	private Button btnRead;
	private Button btnSet;
	private int workPattern;
	private int outputMode;
	
	private LinearLayout llTriggerDelay;
	private LinearLayout llTriggerDelayButton;
	private LinearLayout llTriggerModeAlertTime;
	private LinearLayout llTriggerModeAlertTimeButton;
	private LinearLayout llOutputDataFormat;
	private LinearLayout llOutputDataFormatButton;
	
	private EditText etTriggerDelay;
	private EditText etTriggerModeAlertTime;
	
	private Button btnTriggerDelayRead;
	private Button btnTriggerDelaySet;
	private Button btnTriggerModeAlertTimeRead;
	private Button btnTriggerModeAlertTimeSet;
	private Spinner spinnerOutputDataFormat;
	private Button btnOutputDataFormatRead;
	private Button btnOutputDataFormatSet;
	
	
	R2000ReaderService readerService = new R2000ReaderServiceImpl();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_params_set_work_pattern);
		inital();
	}

	private void inital() {
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		spinnerWorkPattern = (Spinner) findViewById(R.id.spinner_work_pattern);
		btnRead = (Button) findViewById(R.id.btn_work_pattern_read);
		btnSet = (Button) findViewById(R.id.btn_work_pattern_set);
		
		llTriggerDelay = (LinearLayout) findViewById(R.id.ll_work_pattern_trigger_delay);
		llTriggerDelayButton = (LinearLayout) findViewById(R.id.ll_work_pattern_trigger_delay_btn);
		llTriggerModeAlertTime = (LinearLayout) findViewById(R.id.ll_work_pattern_trigger_mode_alert_time);
		llTriggerModeAlertTimeButton = (LinearLayout) findViewById(R.id.ll_work_pattern_trigger_mode_alert_time_btn);
		llOutputDataFormat = (LinearLayout) findViewById(R.id.ll_work_pattern_output_data_format);
		llOutputDataFormatButton = (LinearLayout) findViewById(R.id.ll_work_pattern_output_data_format_btn);
		
		spinnerOutputDataFormat = (Spinner) findViewById(R.id.spinner_work_pattern_output_data_format);
		btnOutputDataFormatRead = (Button) findViewById(R.id.btn_work_pattern_output_data_format_read);
		btnOutputDataFormatSet = (Button) findViewById(R.id.btn_work_pattern_output_data_format_set);
		
		etTriggerDelay = (EditText) findViewById(R.id.et_work_pattern_trigger_delay);
		etTriggerModeAlertTime = (EditText) findViewById(R.id.et_work_pattern_trigger_mode_alert_time);
		
		btnTriggerDelayRead = (Button) findViewById(R.id.btn_work_pattern_trigger_delay_read);
		btnTriggerDelaySet = (Button) findViewById(R.id.btn_work_pattern_trigger_delay_set);
		btnTriggerModeAlertTimeRead = (Button) findViewById(R.id.btn_work_pattern_trigger_mode_alert_time_read);
		btnTriggerModeAlertTimeSet = (Button) findViewById(R.id.btn_work_pattern_trigger_mode_alert_time_set);
		
		spinnerWorkPattern.setOnItemSelectedListener(this);
		spinnerOutputDataFormat.setOnItemSelectedListener(this);
		btnRead.setOnClickListener(this);
		btnSet.setOnClickListener(this);
		btnRead.performClick();
		
		btnTriggerDelayRead.setOnClickListener(this);
		btnTriggerDelaySet.setOnClickListener(this);
		btnTriggerModeAlertTimeRead.setOnClickListener(this);
		btnTriggerModeAlertTimeSet.setOnClickListener(this);
		btnOutputDataFormatRead.setOnClickListener(this);
		btnOutputDataFormatSet.setOnClickListener(this);
	}

	/*****************************************************************************/
	public void shake_activity_back(View v) { // ������ ���ذ�ť
		this.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
		switch (parent.getId()) {
		case R.id.spinner_work_pattern:
			workPattern = position;
			if(position == 2){
				llTriggerDelay.setVisibility(View.VISIBLE);
				llTriggerDelayButton.setVisibility(View.VISIBLE);
				llTriggerModeAlertTime.setVisibility(View.VISIBLE);
				llTriggerModeAlertTimeButton.setVisibility(View.VISIBLE);
				
				llOutputDataFormat.setVisibility(View.GONE);
				llOutputDataFormatButton.setVisibility(View.GONE);
			}else if(position == 1){
				llOutputDataFormat.setVisibility(View.VISIBLE);
				llOutputDataFormatButton.setVisibility(View.VISIBLE);
				
				llTriggerDelay.setVisibility(View.GONE);
				llTriggerDelayButton.setVisibility(View.GONE);
				llTriggerModeAlertTime.setVisibility(View.GONE);
				llTriggerModeAlertTimeButton.setVisibility(View.GONE);
			}else{
				llTriggerDelay.setVisibility(View.GONE);
				llTriggerDelayButton.setVisibility(View.GONE);
				llTriggerModeAlertTime.setVisibility(View.GONE);
				llTriggerModeAlertTimeButton.setVisibility(View.GONE);
				llOutputDataFormat.setVisibility(View.GONE);
				llOutputDataFormatButton.setVisibility(View.GONE);
			}
			break;
		case R.id.spinner_work_pattern_output_data_format:
			outputMode = position;
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> parent) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_work_pattern_read:
			workPatternRead();
			break;
		case R.id.btn_work_pattern_set:
			workPatternSet();
			break;
		case R.id.btn_work_pattern_trigger_delay_read:
			triggerDelayRead();
			break;
		case R.id.btn_work_pattern_trigger_delay_set:
			triggerDelaySet();
			break;
		case R.id.btn_work_pattern_trigger_mode_alert_time_read:
			triggerModeAlertTimeRead();
			break;
		case R.id.btn_work_pattern_trigger_mode_alert_time_set:
			triggerModeAlertTimeSet();
			break;
		case R.id.btn_work_pattern_output_data_format_read:
			outputDataFormatRead();
			break;
		case R.id.btn_work_pattern_output_data_format_set:
			outputDataFormatSet();
			break;
		}
	}

	private void outputDataFormatSet() {
		try {
			boolean result = readerService.setOutputMode(ReaderUtil.readers, (byte)(outputMode + 1));
			if(result){
				Toasts.makeTextShort(this, R.string.msg_work_mode_output_data_format_set_succeed);
			}else{
				Toasts.makeTextShort(this, R.string.msg_work_mode_output_data_format_set_failed);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void outputDataFormatRead() {
		try {
			int reuslt = readerService.getOutputMode(ReaderUtil.readers);
			if(reuslt > -1 && reuslt < 5){
				reuslt = reuslt - 1;
				spinnerOutputDataFormat.setSelection(reuslt);
				Toasts.makeTextShort(this, R.string.msg_work_mode_output_data_format_read_succeed);
				return;
			}
			Toasts.makeTextShort(this, R.string.msg_work_mode_output_data_format_read_failed);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void triggerModeAlertTimeSet() {
		try {
			String time = etTriggerModeAlertTime.getText().toString();
			if(Regex.isDecNumber(time)){
				int reulst = Integer.parseInt(time);
				if(reulst > -1 && reulst <= 255){
				  boolean total = readerService.setTriggerModeAlertTime(ReaderUtil.readers,(byte)reulst);
					if (total) {
						Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_mode_alert_time_set_succeed);
						return;
					}
				}
			}
			Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_mode_alert_time_set_failed);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	private void triggerModeAlertTimeRead() {
		
	}

	private void triggerDelaySet() {
		try {
			String time = etTriggerDelay.getText().toString();
			if(Regex.isDecNumber(time)){
				int reulst = Integer.parseInt(time);
				if(reulst > -1 && reulst <= 255){
					boolean total = readerService.setTriggerModeDelayTime(ReaderUtil.readers, (byte)reulst);
					if(total){
						Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_delay_set_succeed);
						return;
					}
				}
			}
			Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_delay_set_failed);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
	}

	private void triggerDelayRead() {
		try {
			int time = readerService.getTriggerModeDelayTime(ReaderUtil.readers);
			if(time > -1){
				etTriggerDelay.setText(String.valueOf(time));
				Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_delay_read_succeed);
				return;
			}
			Toasts.makeTextShort(this, R.string.msg_work_mode_trigger_delay_read_failed);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void workPatternSet() {
		try {
			boolean result = readerService.setWorkMode(ReaderUtil.readers, workPattern + 1);
			if(result){
				Toasts.makeTextShort(this, R.string.msg_work_mode_set_succeed);
			}else{
				Toasts.makeTextShort(this,R.string.msg_work_mode_set_failed);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void workPatternRead() {
		try {
			int result = readerService.getWorkMode(ReaderUtil.readers);
			if(result > -1 && result < 3){
				spinnerWorkPattern.setSelection(result - 1);
				Toasts.makeTextShort(this,R.string.msg_work_mode_read_succeed);
			}else{
				Toasts.makeTextShort(this, R.string.msg_work_mode_read_failed);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}